package cn.lili.modules.member.entity.dos;

import cn.lili.common.enums.ClientTypeEnum;
import cn.lili.common.security.enums.SceneEnums;
import cn.lili.common.security.sensitive.Sensitive;
import cn.lili.common.security.sensitive.enums.SensitiveStrategy;
import cn.lili.common.utils.BeanUtil;
import cn.lili.modules.member.entity.dto.UserInfoDTO;
import cn.lili.modules.permission.entity.dto.UserAdminDTO;
import cn.lili.mybatis.model.BaseSceneEntity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.springframework.format.annotation.DateTimeFormat;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

/**
 * 用户
 *
 * @author liushuai(liushuai711 @ gmail.com)
 * @version v4.0
 * @Description:
 * @since 2022/8/23 19:26
 */
@EqualsAndHashCode(callSuper = true)
@Data
@TableName("li_user")
@ApiModel(value = "用户")
@SuperBuilder
@AllArgsConstructor
@NoArgsConstructor
public class User extends BaseSceneEntity implements Serializable {

    private static final long serialVersionUID = -7654006987374239274L;


    @ApiModelProperty(value = "用户用户名")
    private String username;

    @NotEmpty(message = "手机号码不能为空")
    @ApiModelProperty(value = "手机号码", required = true)
    @Sensitive(strategy = SensitiveStrategy.PHONE)
    private String mobile;

    @ApiModelProperty(value = "用户密码")
    private String password;

    @ApiModelProperty(value = "昵称")
    private String nickName;

    @Min(message = "用户性别参数错误", value = 0)
    @ApiModelProperty(value = "用户性别,1为男，0为女,-1 未知")
    private Integer sex;

    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @ApiModelProperty(value = "用户生日")
    private Date birthday;

    @ApiModelProperty(value = "头像")
    private String face;

    @ApiModelProperty(value = "启用状态")
    private Boolean enable;

    @ApiModelProperty(value = "是否是超级管理员 超级管理员/普通管理员")
    private Boolean isSuper = false;

    /**
     * @see ClientTypeEnum
     */
    @ApiModelProperty(value = "客户端")
    private String clientEnum;

    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty(value = "最后一次登录时间")
    private Date lastLoginDate;


    @ApiModelProperty(value = "所属部门id")
    private String departmentId;

    @ApiModelProperty(value = "角色id集合")
    private String roleIds;

    @ApiModelProperty(value = "场景昵称")
    private String extendName;

    @ApiModelProperty(value = "是否是店主", hidden = true)
    private Boolean shopkeeper = false;

    @ApiModelProperty(value = "城市分站Id")
    private String urbanSubstationId;

    @ApiModelProperty(value = "是否允许从1688采集商品")
    private Boolean grabAlibabaOpen=false;

    @ApiModelProperty(value = "审核状态：0未审核，1审核中，2审核完成")
    private Integer auditStatus;

    @ApiModelProperty(value = "openId")
    private String openId;

    @ApiModelProperty(value = "account")
    private String account;

    @ApiModelProperty(value = "fy_name")
    private String fyName;

    @ApiModelProperty(value = "绿色积分池冻结状态，true/false 冻结/未冻结")
    private Boolean greenFrozen;

    @ApiModelProperty("上级ID")
    private String parentId;

    @ApiModelProperty("上级昵称")
    private String parentUsername;

    @ApiModelProperty("是否是店长/分销员: 0,不是 1,是")
    private Boolean isDistribution;
    @ApiModelProperty("店长身份")
    @TableField(exist = false)
    private String isDistributionName;
    @ApiModelProperty("订单数量")
    @TableField(exist = false)
    private Integer orderCount;
    @ApiModelProperty("本月消费金额")
    @TableField(exist = false)
    private Double currentConsumption;
    @ApiModelProperty("累计消费金额")
    @TableField(exist = false)
    private Double cumulativeConsumption;

    /**
     * 用户类型
     */
    @TableField(exist = false)
    private Integer type;

    /**
     * 本月金额
     */
    @TableField(exist = false)
    private BigDecimal money;

    @ApiModelProperty("是否免密支付：0不是，1是")
    private Boolean isPassword;

    @ApiModelProperty("所属工会ID")
    private String unionId;


    @ApiModelProperty("上级绑定时间")
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date parentBindTime;

    public String getExtendName() {
        if (extendName == null) {
            return "";
        }
        return extendName;
    }

    public User(String username, String password, SceneEnums sceneEnums) {

        this.defaultInitialize();
        this.setScene(sceneEnums.value());
        this.username = username;
        this.password = password;
        this.nickName = username;
    }

    public User(String username, String password, String mobile, SceneEnums sceneEnums) {

        this.defaultInitialize();

        this.setScene(sceneEnums.value());
        this.username = username;
        this.password = password;
        this.mobile = mobile;
        this.nickName = username;
    }

    public User(UserInfoDTO userInfoDTO) {
        this.defaultInitialize();
        BeanUtil.copyProperties(userInfoDTO, this);
        this.setScene(userInfoDTO.getScene().value());
        this.setRoleIds(userInfoDTO.getRoleIdsStr());
    }

    public User(UserAdminDTO userAdminDTO) {
        this.defaultInitialize();
        BeanUtil.copyProperties(userAdminDTO, this);
        this.setScene(SceneEnums.MANAGER.value());
    }

    /**
     * 初始化默认值
     */
    private void defaultInitialize() {
        this.enable = true;
        this.sex = -1;
        this.lastLoginDate = new Date();
        this.birthday = new Date();
    }
}
